/**
 * jims
 */
package com.jims.master.modules.base.user.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.modules.base.user.dao.MasterContactinfoDao;
import com.jims.master.modules.base.user.entity.MasterContactinfo;
import com.jims.master.modules.sys.service.SysLoginNameService;
import com.jims.master.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户子信息Service
 * @author jict
 * @version 2017-11-15
 */
@Service
@Transactional(readOnly = true)
public class MasterContactinfoService extends CrudService<MasterContactinfoDao, MasterContactinfo> {

    /*@Autowired
    private SysLoginNameService sysLoginNameService*/;
    @Autowired
    private UserService userService;

    public MasterContactinfo getContactinfoByBasId(String baseId) {
        return dao.getContactinfoByBasId(baseId);
    }

    public MasterContactinfo findByEmail(String numemail) {
        return dao.findByEmail(numemail);
    }

    public MasterContactinfo findByMobliePhone(String numemail) {
        return dao.findByMobliePhone(numemail);
    }

   /* @Transactional(readOnly = false)
    public int updatePhoneById(String mobilePhone, String baseInfo,String oldMobilePhone) {
        String oldUserId=sysLoginNameService.findValidate(mobilePhone);
        User user = UserUtils.getUser();
//      旧的登录信息
        List<SysLoginName> oldSysLogin=sysLoginNameService.findByUserId(user.getId());

        SysLoginName sysLoginName=new SysLoginName();
        sysLoginName.setUser(user);
        sysLoginName.setLoginName(mobilePhone);
        sysLoginName.setType("0");
        sysLoginName.setDelFlag("0");
//        判断是不是重置邮箱
        if(oldMobilePhone!=null&&!(oldMobilePhone.equals(""))){
//          判断登录表中有没有这条登录数据，有的话更新绑定的，没有存入新绑定的数据
            String oldSysLoginName=sysLoginNameService.findValidate(oldMobilePhone);
            if(oldSysLoginName==null||oldSysLoginName.equals("")){
                sysLoginNameService.save(sysLoginName);
            }else{
//              更新存在旧的登录信息的与之相关的这一天数据

                for(int i=0;i<oldSysLogin.size();i++){
                    if(oldSysLogin.get(i).getLoginName().equals(oldMobilePhone)){
                        oldSysLogin.get(i).setLoginName(mobilePhone);
                        sysLoginNameService.update(oldSysLogin.get(i));
                    }
                }
                if(oldSysLogin.size()==1){
                    user.setCurrentLogin(mobilePhone);
                    userService.update(user);
                }

            }
        }else{
//          防止原来注册的登录名是新绑定的邮箱
            if(oldUserId==null||oldUserId.equals("")){
                sysLoginNameService.save(sysLoginName);
            }
        }


        return dao.updatePhoneById(mobilePhone, baseInfo);
    }

    @Transactional(readOnly = false)
    public int updateEmailById(String email, String baseInfo,String oldEmail) {
        String oldUserId=sysLoginNameService.findValidate(email);
        User user = UserUtils.getUser();
        List<SysLoginName> oldSysLogin=sysLoginNameService.findByUserId(user.getId());

        SysLoginName sysLoginName=new SysLoginName();
        sysLoginName.setUser(user);
        sysLoginName.setLoginName(email);
        sysLoginName.setType("3");
        sysLoginName.setDelFlag("0");
//        判断是不是重置邮箱
        if(oldEmail!=null&&!(oldEmail.equals(""))){
//          判断登录表中有没有这条登录数据，有的话更新绑定的，没有存入新绑定的数据
            String oldSysLoginName=sysLoginNameService.findValidate(oldEmail);
            if(oldSysLoginName==null||oldSysLoginName.equals("")){
                sysLoginNameService.save(sysLoginName);
            }else{
//              更新存在旧的登录信息的与之相关的这一天数据
                for(int i=0;i<oldSysLogin.size();i++){
                    if(oldSysLogin.get(i).getLoginName().equals(oldEmail)){
                        oldSysLogin.get(i).setLoginName(email);
                        sysLoginNameService.update(oldSysLogin.get(i));
                    }
                }
                if(oldSysLogin.size()==1){
                    user.setCurrentLogin(email);
                    userService.update(user);
                }
            }
        }else{
//          防止原来注册的登录名是新绑定的邮箱
            if(oldUserId==null||oldUserId.equals("")){
                sysLoginNameService.save(sysLoginName);
            }
        }
        return dao.updateEmailById(email, baseInfo);
    }

    @Transactional(readOnly = false)
    public int insert(MasterContactinfo contactInfo) {
        return dao.insert(contactInfo);
    }

    @Transactional(readOnly = false)
    public int update(MasterContactinfo contactInfo) {
        return dao.update(contactInfo);
    }


    public MasterContactinfo get(String contactInfoId) {
        return super.get(contactInfoId);
    }

    @Transactional(readOnly = false)
    public int delete(MasterContactinfo contact) {
        return super.delete(contact);
    }*/

    public MasterContactinfo getContacBybaseInfo(String baseid) {
        return dao.getContacBybaseInfo(baseid);
    }

    /*/按身份证和手机查询
    public List<MasterContactinfo> findListByCDAndPhone(MasterContactinfo contactInfo) {
        return dao.findListByCDAndPhone(contactInfo);
    }*/

}